package com.google.code.yargon.ui.lighting;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Statement;

import org.apache.log4j.Logger;

import com.google.code.yargon.db.Dao;
import com.google.code.yargon.util.LogUtil;

/**
 * LightSource class - can be saved to database for quicker loading (regeneration takes time)
 * 
 * Database:
 * 
 * table lightsource
 *  id int
 *  radius int
 * 
 * table ray
 *  id int
 *  lightsource int refers to lightsource.id
 *  x int
 *  y int
 *  
 * table intersection
 *  id int
 *  ray int refers to ray.id
 *  on_target boolean
 *  x int
 *  y int
 *  
 * table intersection_point
 *  id int
 *  intersection int refers to intersection.id
 *  x int
 *  y int
 * 
 * @author Mika Myllynen
 *
 */
public class LightSource {
	
	private Ray[] lightRays;
	private Logger log = LogUtil.getInstance();
	
	public LightSource(int radius) {
		lightRays = RayCast.calculateRays(radius);
	}
	
	public Ray[] getRays() {
		return lightRays;
	}
	
	public void save() {
		
	}
	
	private void init() {
		
		Connection conn = Dao.getInstance().getConnection("lightsources.dat");
		
		try {
			Statement stmt = conn.createStatement();
			
			// Tables
			stmt.executeUpdate("create table if not exist lightsource (id integer, radius integer)");
			stmt.executeUpdate("create table if not exist ray (id integer, lightsource integer, x integer, y integer)");
			stmt.executeUpdate("create table if not exist intersection (id integer, ray integer, on_target integer, x integer, y integer)");
			stmt.executeUpdate("create table if not exist intersection_point (id integer, intersection integer, x integer, y integer)");
		}
		
		catch(Exception e) {
			log.error("Could init database: "+e.getMessage());
		}

	}
}
